/////////////////////////////////////////////////////////////////////////////
// File: NetConnect.h
//
// Desc:
//
/////////////////////////////////////////////////////////////////////////////

#include "../JWTS/INCLUDE/LEGACYREPLACER.HPP"
#include <wchar.h>

#ifndef NETCLIENT_H
#define NETCLIENT_H
#define DISPLAY_REFRESH_RATE        250
#define TIMERID_DISPLAY_HOSTS       1
#define TIMERID_CONNECT_COMPLETE    2
#define NCW_S_FORWARD      ( (HRESULT) 0x01000001 )  // Dialog success, so go forward
#define NCW_S_BACKUP       ( (HRESULT) 0x01000002 )  // Dialog canceled, show previous dialog
#define NCW_S_QUIT         ( (HRESULT) 0x01000003 )  // Dialog quit, close app
#define NCW_S_LOBBYCONNECT ( (HRESULT) 0x01000004 )  // Dialog connected from lobby, connect success
/////////////////////////////////////////////////////////////////////////////

class IDirectPlay8Client;
class IDirectPlay8Address;

#define WINAPI

class CNetClientWizard
{
 public:
    CNetClientWizard( HINSTANCE hInst, TCHAR* strAppName, GUID* pGuidApp,int );
    virtual ~CNetClientWizard();
    HRESULT WINAPI MessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer );
    HRESULT Init( IDirectPlay8Client* pDPClient);
    HRESULT DoConnectWizard();
    void    SetPlayerName( TCHAR* strPlayerName )  { wcsncpy( m_strLocalPlayerName, strPlayerName,_countof(m_strLocalPlayerName) ); m_strLocalPlayerName[ MAX_PATH-1 ] = 0; }
    void    SetDefaultPort( DWORD dwDefaultPort )  { m_dwPort = dwDefaultPort; }
    TCHAR*  GetPlayerName()                        { return m_strLocalPlayerName; }
    VOID    SessionsDlgInitListbox( QWidget* hDlg );
    HRESULT SessionsDlgEnumHosts( QWidget* hDlg );
    HRESULT SessionsDlgJoinGame( QWidget* hDlg );
    DPNHANDLE GetAsyncHandle()      { return m_hConnectAsyncOp; } ;
 protected:
    struct DPHostEnumInfo
    {
        DWORD                 dwRef;
        DPN_APPLICATION_DESC* pAppDesc;
        IDirectPlay8Address* pHostAddr;
        IDirectPlay8Address* pDeviceAddr;
        TCHAR                szSession[MAX_PATH];
        DWORD                dwLastPollTime;
        BOOL                 bValid;
        DPHostEnumInfo*      pNext;
    };
    static INT_PTR CALLBACK StaticSessionsDlgProc( QWidget*, UINT, WPARAM, LPARAM );
    INT_PTR CALLBACK SessionsDlgProc( QWidget*, UINT, WPARAM, LPARAM );
    HRESULT SessionsDlgNoteEnumResponse( PDPNMSG_ENUM_HOSTS_RESPONSE pEnumHostsResponse );
    VOID    SessionsDlgExpireOldHostEnums();
    HRESULT SessionsDlgDisplayEnumList( QWidget* hDlg );
    HRESULT SessionsDlgCreateGame( QWidget* hDlg );
    VOID    SessionsDlgEnumListCleanup();
    IDirectPlay8Client*     m_pDPClient;
    CRITICAL_SECTION        m_csHostEnum;
    GUID                    m_guidApp;
    DWORD                   m_dwPort;
    HRESULT                 m_hrDialog;
    QWidget*                m_hDlg;
    HINSTANCE               m_hInst;
    DPHostEnumInfo          m_DPHostEnumHead;
    TCHAR                   m_strAppName[MAX_PATH];
    TCHAR                   m_strLocalPlayerName[MAX_PATH];
    BOOL                    m_bSearchingForSessions;
    BOOL                    m_bEnumListChanged;
    DPNHANDLE               m_hEnumAsyncOp;
    DWORD                   m_dwEnumHostExpireInterval;
    BOOL                    m_bConnecting;
    DPNHANDLE               m_hConnectAsyncOp;
    HANDLE                  m_hConnectCompleteEvent;
    HRESULT                 m_hrConnectComplete;
    GUID                    m_guidServicePr;
    int                     m_iServicePr;
};
#endif // NETCLIENT_H
/////////////////////////////////////////////////////////////////////////////

